package com.example.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.exam.entity.DTO.OptionsDTO;
import com.example.exam.entity.DTO.QuestionDTO;
import com.example.exam.entity.Topic;
import com.example.exam.entity.VO.TopicList;
import com.example.exam.entity.compound.Question;
import com.example.exam.entity.pojo.Option;
import com.example.exam.service.OptionService;
import com.example.exam.service.QuestionServiceImpl;
import com.example.exam.utils.AmazeS3Utils;
import com.example.exam.utils.SynthesisUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhang
 * 题库管理
 */
@RestController
@RequestMapping("/question")
@Log4j2
public class QuestionController {
    @Autowired
    private QuestionServiceImpl questionService;
    @Autowired
    private AmazeS3Utils amazeS3Utils;
    @Autowired
    private OptionService optionService;


    /*
     * 添加单个试题
     * 或使用Map接收 options 参数
     */
//    @PostMapping("/add")
//    @Transactional
//    public ResponseEntity<String> add(@RequestBody QuestionDTO question, @RequestParam("questionImg") MultipartFile questionImg, @RequestParam("optionImg") List<MultipartFile> optionImg, @RequestParam("option") List<String> option) {
//        if (SynthesisUtils.questionType(question.getType())) {
//            return ResponseEntity.badRequest().body("题目类型错误");
//        }
//        QuestionBank questionbank = new QuestionBank();
//        BeanUtils.copyProperties(question, questionbank);
//        log.info("------上传图片开始------");
//        if (questionImg.isEmpty()) {
//            log.info("------题目图片为空------");
//            questionbank.setImg(null);
//        } else {
//            //设置图片路径
//            questionbank.setImg(amazeS3Utils.uploadImg(questionImg));
//        }
//        //保存试题，返回试题id
//        Integer id = questionService.addQuestion(questionbank);
//        List<Option> options = new ArrayList<>(option.size());
//        for (int i = 0; i < option.size(); i++) {
//            Option option1 = new Option();
//            option1.setId(id);
//            option1.setText(option.get(i));
//            if (optionImg.get(i).isEmpty()) {
//                option1.setImg(null);
//            } else {
//                option1.setImg(amazeS3Utils.uploadImg(optionImg.get(i)));
//            }
//            options.add(option1);
//        }
//        if (optionService.insertOptionList(options)) {
//            return ResponseEntity.badRequest().body("选项添加错误");
//        }
//        log.info("------上传图片结束------");
//        return ResponseEntity.ok("添加成功");
//    }

    @Transactional
    @PostMapping("/add")
    public ResponseEntity<String> addQuestion(@ModelAttribute QuestionDTO question, @RequestParam("questionImg") MultipartFile questionImg,
                                              @ModelAttribute List<OptionsDTO> options) {
        if (SynthesisUtils.questionType(question.getType())) {
            return ResponseEntity.badRequest().body("题目类型错误");
        }
        Topic questionbank = new Topic();
        BeanUtils.copyProperties(question, questionbank);
        if (questionImg.isEmpty()) {
            questionbank.setImg(null);
        } else {
            //设置图片路径
            questionbank.setImg(amazeS3Utils.uploadImg(questionImg));
        }
        questionService.save(questionbank);
        List<Option> optionList = new ArrayList<>(options.size());
        if (!options.isEmpty()) {
            options.forEach(i -> {
                Option option = new Option();
                BeanUtils.copyProperties(i, option);
                if (i.getOptionImg().isEmpty()) {
                    option.setUrl(null);
                } else {
                    option.setUrl(amazeS3Utils.uploadImg(i.getOptionImg()));
                }
                option.setId(null);
                optionList.add(option);
            });
        }
        optionService.saveBatch(optionList);
        return ResponseEntity.ok("添加成功");
    }

    /**
     * 获取试题 id，type，content 用于展示试题列表
     */
    @GetMapping("/list")
    public ResponseEntity<IPage<TopicList>> list(@RequestParam Integer page, @RequestParam Integer size) {
        return ResponseEntity.ok(questionService.getQuestion(page, size));
    }

    /**
     * 获取题目信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Question> question(@PathVariable Integer id) {
        return ResponseEntity.ok(questionService.getQuestionById(id));
    }

    /**
     * 逻辑删除题目
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteQuestion(@PathVariable Integer id) {
        QueryWrapper<Topic> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Topic topic = questionService.getById(id);
        if (topic == null) {
            return ResponseEntity.badRequest().body("题目不存在");
        }
        topic.setStatus(Topic.QUESTION_STATUS_DELETE);
        boolean result = questionService.update(topic, wrapper);
        if (result) {
            return ResponseEntity.ok("修改成功");
        } else {
            return ResponseEntity.badRequest().body("修改失败");
        }
    }

    //revise


}
